package leetcode.editor.cn;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class P437PathSumIii {
    public static void main(String[] args) {
        Solution p437 = new P437PathSumIii().new Solution();
        Solution.TreeNode treeNode = new P437PathSumIii().new Solution().buildTree(new Integer[]{1000000000, 1000000000, null, 294967296, null, 1000000000, null, 1000000000, null, 1000000000});
        System.out.println(p437.pathSum(treeNode, 0));
        
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(Integer[] arr) {
        int index = 0;
        TreeNode root = new TreeNode(arr[index]);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if (++index < arr.length) {
                Integer left = arr[index];
                if (left != null) {
                    poll.left = new TreeNode(left);
                    queue.add(poll.left);
                }
            }
            if (++index < arr.length) {
                Integer right = arr[index];
                if (right != null) {
                    poll.right = new TreeNode(right);
                    queue.add(poll.right);
                }
            }

        }
        return root;
    }

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return 0;
        }
        int ret = path(root, targetSum - root.val);
        ret += pathSum(root.left, targetSum);
        ret += pathSum(root.right, targetSum);
        return ret;
    }

    public int path(TreeNode root, long target) {
        int ret = 0;
        if (root == null) {
            return ret;
        }
        if (0 == target) {
            ret++;
        }
        if (root.left != null) {
            ret += path(root.left, target - root.left.val);
        }
        if (root.right != null) {
            ret += path(root.right, target - root.right.val);
        }
        return ret;
    }

    /**
     * 通过前缀树
     *
     * @param root
     * @param targetSum
     * @return
     */
    public int pathSumPre(TreeNode root, int targetSum) {
        Map<Long, Integer> prefix = new HashMap<Long, Integer>();
        prefix.put(0L, 1);
        return dfs(root, prefix, 0, targetSum);
    }

    public int dfs(TreeNode root, Map<Long, Integer> prefix, long curr, int targetSum) {
        if (root == null) {
            return 0;
        }

        int ret = 0;
        curr += root.val;

        ret = prefix.getOrDefault(curr - targetSum, 0);
        prefix.put(curr, prefix.getOrDefault(curr, 0) + 1);
        ret += dfs(root.left, prefix, curr, targetSum);
        ret += dfs(root.right, prefix, curr, targetSum);
        prefix.put(curr, prefix.getOrDefault(curr, 0) - 1);

        return ret;
    }

    public  class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}